﻿GY.Bill.Friend = {
    SaveGroup: function () {
        $.ajax({
            type: "Post",
            url: $("#btnSaveGroup").attr("data-url"),
            data: { GroupName: $("#txtGroupName").val() },
            dataType: "json",
            success: function (result) {
                if (result.code == 1) {
                   /* GY.Bill.Common.ShowSuccessTip();
                    $("#btnSaveGroup").val("");
                    $("#div_group_list").find("a").before("<span data-val=\"" + result.data.GroupId + "\" >" + result.data.GroupName + "</span>");
                    $("#ul_group_list").append("<li><a href=\"javascript:;\" data-val=\"" + result.data.GroupId + "\"><i class=\"icon\"></i>" + result.data.GroupName + "</a></li>");
                    $(".shade").hide();
                    $("#div_group").hide();
                    $("#txtGroupName").val("");*/
                    window.location.reload();
                } else {
                    alert(result.msg);
                }
            }
        });
    },
    MoveGroup: function (obj, friendId, groupId, relationId, groupName) {
        $.ajax({
            type: "Post",
            url: $(obj).attr("data-url"),
            data: { FriendId: friendId, RelationId: relationId, GroupId: groupId },
            dataType: "json",
            success: function (result) {
                if (result.code == 1) {
                    var $member = $("#div_group_member_list").find(".member" + friendId);
                    $member.text(groupName);
                    $member.attr("data-groupid", groupId);
                    $("#div_move_group").hide();
                }
            }
        });
    },
    LoadGroupFriend: function (groupId) {
        var $list = $("#div_group_member_list");
        $.ajax({
            type: "Post",
            url: $list.attr("data-url"),
            data: { groupId: groupId },
            dataType: "html",
            success: function (result) {
                $list.html(result);
            }
        });
    },
    removeGroup: function (groupId) {
        $.ajax({
            type: "Post",
            url: $("#div_friend").attr("data-remove-group"),
            data: { groupId: $("#div_friend").attr("data-val") },
            dataType: "json",
            success: function (result) {
                if (result.code == 1) {
                    //删除成功
                    window.location.reload();
                } else {
                    alert(result.msg);
                }
            }
        });
    },
    removeFriend: function (friendId) {
        $.ajax({
            type: "Post",
            url: $("#div_friend").attr("data-remove-friend"),
            data: { friendId: $("#div_friend").attr("data-val") },
            dataType: "json",
            success: function (result) {
                if (result.code == 1) {
                    //删除成功
                    window.location.reload();
                } else {
                    alert(result.msg);
                }
            }
        });
    }
};

(function () {
    var winHeight = $(window).height();
    $(function () {
        $("#menuMember").addClass("active");

        $(".shade").on("tap", function () {
            $(".shade").hide();
            $(".mask").hide();
        });
        //分组
        $(".friend").on("tap", ".list .btnGroup", function () {
            var top = $(this).offset().top;
            $("#div_move_group").show();
            $("#div_move_group").find(".arrow").css("top", top);

            //设置分组选中
            $("#ul_group_list a").removeClass("active");

            var groupId = $(this).attr("data-groupid");
            var friendId = $(this).attr("data-val");
            var relationId = $(this).attr("data-relationid");

            $("#ul_group_list li .group" + groupId).addClass("active");

            var $group = $("#div_friend");
            $group.attr("data-friendid", friendId);
            $group.attr("data-relationid", relationId);
        });

        $("#div_group_list").on("tap", "span", function () {
            var $this = $(this);
            var groupId = $this.attr("data-val");

            $this.parent().find("span").removeClass("active");
            $this.addClass("active");
            //重新加载好友信息
            GY.Bill.Friend.LoadGroupFriend(groupId);
        });
        $("#ul_group_list").height() < winHeight - 43 - 50 ? $(".friend .list").height(winHeight - 43 - 50) : $("#ul_group_list").height("auto");
        $(".branch").on("tap", function () {
            $(".branch").hide();
        });
        $(".change").on("tap", function (event) {
            event.stopPropagation();
        });

        $("#btnShowAddGroup").on("tap", function () {
            fnShade($(".shade"));
            $(".shade").show();
            $("#div_group").show();
        });

        //保存分组
        $("#btnSaveGroup").on("tap", function () {
            GY.Bill.Friend.SaveGroup();
        });

        //设置分组
        $("#ul_group_list").on("tap", "a", function () {
            $("#ul_group_list a").removeClass("active");
            $(this).addClass("active");

            var friendId = $("#div_friend").attr("data-friendid");
            var groupId = $(this).attr("data-val");
            var relationId = $("#div_friend").attr("data-relationid");
            var groupName = $(this).text();
            GY.Bill.Friend.MoveGroup($(this), friendId, groupId, relationId, groupName);
        });

        $(".mask_group").on("focus", "input", function () {
            $(".mask_group").css("position", "relative");
            fnShade($(".shade"));
        });
        $(".mask_group").on("blur", "input", function () {
            $(".mask_group").css("position", "fixed");
            fnShade($(".shade"));
        });

        $(".friend").on("swipeLeft", ".list li", function () {
            $("#div_friend").attr("data-val", $(this).find(".btnGroup").attr("data-val"));
            ShowPopup("确认要删除好友【" + ($(this).find("p").text()) + "】？", "删除", "removeFriend");
        });
        $(".friend").on("longTap", ".group span", function () {
            var groupId = $(this).attr("data-val");
            $("#div_friend").attr("data-val", groupId);
            if (groupId != "0") {
                ShowPopup("确认要删除分组【" + ($(this).text()) + "】？", "删除", "removeGroup");
            }
        });

        //确认发布票据
        $("#btnSure").on("tap", function () {
            var command = $(this).attr("command");

            switch (command) {
                case "removeGroup":
                    GY.Bill.Friend.removeGroup();
                    break;
                case "removeFriend":
                    GY.Bill.Friend.removeFriend();
                    break;
                default:
                    break;
            }

        });
    });
})();

